// 区块共识：编织区块链的算法，包含创世区块以及调整全网挖矿难度。
// “孤块”（Orphan Block）的概念，即未被主链采纳的区块会被丢弃，其中的交易会重新回到交易池等待被打包。这也是需要注意的一点。
//      孤块交易状态：部分可能已被主链包含，未包含的会重新进入交易池。
//      交易广播机制：节点自动回收并广播未确认交易，无需矿工手动操作。

use thiserror::Error;

use bc_base::{BlockId, block::BlockError};
use bc_lib::top_storage::TopStorageError;

use crate::{block::block_index::BlockIndexError, transaction::TransactionConsensusError};

mod block_impl;
mod block_index;
mod orphan_blocks;

pub(super) use crate::block::block_impl::BlockConsensus;

#[derive(Error, Debug, PartialEq)]
pub enum BlockConsensusError {
    #[error("Transaction Error from bc_base")]
    BlockError(#[from] BlockError),

    #[error("Block Index Error")]
    BlockIndexError(#[from] BlockIndexError),

    // already have the block
    #[error("Block Already Exists")]
    BlockAlreadyExists(BlockId),

    // already have the orphan block
    #[error("Orphan Block Already Exists")]
    OrphanBlockAlreadyExists(BlockId),

    // need get blocks from network
    #[error("Need Get Blocks From Network")]
    NeedGetBlockFromNetwork(BlockId, BlockId), // start_hash, end_hash

    // prev block not found
    #[error("Prev Block Not Found")]
    PrevBlockNotFound(BlockId),

    #[error("Timestamp Too Old")]
    TimestampTooOld,

    // contains non-final transaction
    #[error("Contains Non-Final Transaction")]
    ContainsNonFinalTransaction,

    // incorrect proof of work
    #[error("Incorrect Proof of Work")]
    IncorrectProofOfWork,

    // TopStorageError
    #[error("TopStorageError")]
    TopStorageError(#[from] TopStorageError),

    // TransactionConsensusError
    #[error("TransactionConsensusError: {0}")]
    TransactionConsensusError(#[from] TransactionConsensusError),
}
